package service

import (
	"fmt"
)

// 查询余额功能
func (cs *Consumer) CheckBalance(username string) {
	consumer := cs.ReadData(username)
	fmt.Printf("%s 用户的余额为 %v", consumer.Name, consumer.Balance)
}

// 转账
func (cs *Consumer) Transfer() {
	fmt.Printf("请输入需要转账的用户：")
	var toconsumer string
	fmt.Scan(&toconsumer)

	if cs.IsExist(toconsumer) {
		toConsumer := cs.ReadData(toconsumer)
		fmt.Printf("请输入需要转账的金额：")
		var money int
		fmt.Scan(&money)

		if money > cs.Balance {
			fmt.Println("余额不足，转账失败！")
		} else {
			cs.Balance -= money
			toConsumer.Balance += money
			cs.SaveData(*cs)
			cs.SaveData(toConsumer)

		}
	} else {
		fmt.Println("用户不存在！")
	}

}

// 还款功能
func (cs *Consumer) Repayment() {
	var repayMoney int
	fmt.Printf("请输入还款金额： ")
	fmt.Scan(&repayMoney)

	if repayMoney > cs.Balance {
		fmt.Println("余额不足，还款失败！")
	} else {
		cs.Balance += repayMoney
		cs.SaveData(*cs)
		fmt.Printf("您成功还款%d ，账户余额为%d", repayMoney, cs.Balance)
	}
}

// 取款功能
func (cs *Consumer) Withdraw() {
	var withdrawMoney int
	fmt.Printf("请输入取款金额： ")
	fmt.Scan(&withdrawMoney)

	if withdrawMoney > cs.Balance {
		fmt.Println("穷逼，你没这么多钱！")
	} else {
		cs.Balance -= withdrawMoney
		cs.SaveData(*cs)
		fmt.Printf("您成功还款%d ，账户余额为%d", withdrawMoney, cs.Balance)
	}
}
